package Multithread.Thread.TwoPhaseTermination;

//两阶段终止，简单版
public class twoPhaseTermination01{

    private Thread monitor;

    public void start(){
        monitor = new Thread(()->{
            while (true){
                Thread current =Thread.currentThread();
//                interrupted方法会将isInterrupted置为false,isInterrupted则不会
                if(current.isInterrupted()){
                    System.out.println("Thread monitor is stop");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    System.out.println("Thread monitor log");
                } catch (InterruptedException e) {
                    e.printStackTrace();
//                    isInterrupted方法会将isInterrupted置为false，所以需要在此处在执行一次Interrupt；
                    current.interrupt();
                }
            }
        },"t1");
        monitor.start();
    }

    public void stop(){
        monitor.interrupt();
    }

}
